package com.ccx.coachsite.ui.manage;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.ccx.coachsite.R;
import com.ccx.coachsite.bus.CommonBus;
import com.ccx.coachsite.bus.SharedKey;
import com.ccx.coachsite.http.BaseResp;
import com.ccx.coachsite.ui.own.OwerAdapterBase;
import com.ccx.coachsite.util.ContactInfo;
import com.ccx.coachsite.util.SharedPreferencesUtil;
import com.ccx.coachsite.util.StringUtil;

public class ManageContactAdapter extends OwerAdapterBase<ContactInfo>{
	public ManageContactAdapter(Context context, List<ContactInfo> list) {
		super(context, list);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if(convertView == null) {
			convertView = mInflater.inflate(R.layout.manage_contact_list_item, parent, false);
			holder = new ViewHolder();
			holder.nName = (TextView) convertView.findViewById(R.id.manage_contact_name);
			holder.nPhone = (TextView) convertView.findViewById(R.id.manage_contact_phone);
			holder.nButton = (Button) convertView.findViewById(R.id.manage_contact_submit_button);
			convertView.setTag(holder);
		}else 
		{
			holder = (ViewHolder) convertView.getTag();
		}
		ContactInfo cinfo = mList.get(position);
		holder.nName.setText(cinfo.getName());
		holder.nPhone.setText("手机号码:"+cinfo.getPhone());
		if(!cinfo.isApply())
		{
			holder.nButton.setText("可认领");
			holder.nButton.setTextColor(mContext.getResources().getColor(R.color.blue_title_color));
			holder.nButton.setOnClickListener(new OnIntiveListener());	
			holder.nButton.setTag(cinfo.getPhone());
		}
		else
		{
			holder.nButton.setText("已认领");
			holder.nButton.setEnabled(false);
			holder.nButton.setTextColor(mContext.getResources().getColor(R.color.dark_gray));
		}
			
		
		return convertView;
	}
	
	public void doInvate(String mobiles)
	{
		deleteContactBus bus = new deleteContactBus(mContext);
		String userId = SharedPreferencesUtil.getValue(mContext,
				SharedKey.Shared_UserId_Key, "");
		bus.setAddCustomParam(mobiles, userId, 0, 0, 0, "");
		//bus.setInviteParam(userId, mobiles);
		bus.doTask();
	}
	
	class OnIntiveListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			Object obj = v.getTag();
			if(obj != null)
			{
				String mobile = v.getTag().toString();
				doInvate(mobile);
			}
		}
	}
	
	public void doDeleteContact(String mobile)
	{
		if(StringUtil.isEmpty(mobile))
			return;
		
		for(int i = mList.size() - 1; i>=0; i--)
		{
			if(mList.get(i).getPhone().equals(mobile))
			{
				mList.remove(i);
			}
		}
		this.notifyDataSetChanged();
	}
	
	class deleteContactBus extends CommonBus {
		private String mobile;
		public void setId(String mobile)
		{
			this.mobile = mobile;
		}
		
		public deleteContactBus(Context context) {
			super(context);
		}

		@Override
		public void callBack(int taskType, BaseResp response,
				String responseJson) {
			super.callBack(taskType, response, responseJson);

			if (response.isSuccess()) {
				doDeleteContact(mobile);
			} else {

			}
		}
	}

	
	class ViewHolder {
		TextView nName;
		TextView nPhone;
		Button nButton;
	}

}
